﻿using AutoMapper;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.Delparment;
using B.S.BasicData.Read.DTOS;
using MediatR;
using System.Collections.Generic;

namespace B.S.BasicData.Read.Apilication.Handler.Delparment
{
    public class SetDelparmentusernameHandler : IRequestHandler<SetDelparmentusernameCommon, ApiResult<List<SetuserDTO>>>
    {
        private readonly IBaseRepository<UserModel> userRepo;
        private readonly IBaseRepository<AccountRoleModel> accountroleRepo;
        private readonly IMapper mapper;

        public SetDelparmentusernameHandler(IBaseRepository<UserModel> userRepo,IBaseRepository<AccountRoleModel> accountroleRepo,IMapper mapper)
        {
            this.userRepo = userRepo;
            this.accountroleRepo = accountroleRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<SetuserDTO>>> Handle(SetDelparmentusernameCommon request, CancellationToken cancellationToken)
        {
            ApiResult<List<SetuserDTO>> result=new ApiResult<List<SetuserDTO>>();

            var res = accountroleRepo.GetAll().Where(x => x.RoleId == request.RoleId).Select(x=>x.UserId);
            var userlist = userRepo.GetAll().Where(x=>res.Contains(x.UserId));

            var model=mapper.Map<List<SetuserDTO>> (userlist);

            result.Code=ApiEnum.Success;
            result.Message = "显示成功";
            result.Data = model;
            return Task.FromResult(result);

        }
    }
}
